<template>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="基础详情" name="first">
          <slot></slot>
        </el-tab-pane>
        <el-tab-pane label="测试数据" name="second" v-if="isAuto">
            <div v-loading="loading" class="testDataContent">
              <div class="custom-title"><i class="el-icon-vertical-bar mr10"></i>基本信息</div>
              <avue-form v-model="testDataDetail" :option="testDataDetailOption"></avue-form>
              <div class="custom-title"><i class="el-icon-vertical-bar mr10"></i>测试数据</div>
              <avue-crud
                v-if="testDataDetailResult && testDataDetailResult.length > 0"
                :option="testDataDetailListOption"
                :data="testDataDetailResult"
              >
              </avue-crud>
              <div class="custom-title"><i class="el-icon-vertical-bar mr10"></i>附件</div>
              <div v-if="fileListHtml" style="display: flex; width: 100%; height: 200px; overflow: auto" v-html="fileListHtml"></div>
            </div>
        </el-tab-pane>
      </el-tabs>
  </div>
</template>

<script>
import { getDetail } from '@/api/repair/failureconfirm';

export default {
  name: 'RepairDetail',
  props:{
    rowData:{
        type:Object,
        default:()=>{}
    },
    isAuto:{
        type:Boolean,
        default:false
    },
    activeName:{
        type:String,
        default:"first"
    },
    immediately: {
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      loading:false,
      isAuto: false,
      testDataDetail: {},
      testDataDetailOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: '130',
        column: [
          {
            label: '产品序列号',
            prop: 'sequenceNumber',
            disabled: true
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            disabled: true
          },
          {
            label: '产品编码',
            prop: 'partNo',
            disabled: true
          },
          {
            label: '产品名称',
            prop: 'partName',
            disabled: true
          },
          {
            label: '过站工序',
            prop: 'routeName',
            disabled: true
          },
          {
            label: '测试结果',
            prop: 'result',
            disabled: true
          }
        ]
      },
      testDataDetailListOption: {
        height: '200',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        labelWidth: 100,
        border: true,
        index: true,
        viewBtn: false,
        selection: false,
        dialogClickModal: false,
        indexLabel: '序号',
        menu: false,
        refreshBtn: false,
        columnBtn: false,
        addBtn: false,
        column: []
      },
      testDataDetailResult: [],
      fileListHtml: '',
      activeName: 'first',
      viewDialog: false,
      immediately: false,
      rowData: {},
    };
  },
  created() {},
  mounted() {
    if(this.immediately && this.rowData.productSequenceNumber) {
      this.openViewDialog();
    }
  },
  computed: {
  },
  methods: {
    handleClick(tab) {
        if(tab.name=="second"){
            this.openViewDialog();
        }
    },
    openViewDialog() {
      this.testDataDetail = {};
      this.testDataDetailResult = [];
      this.testDataDetailListOption.column = [];
      let { productSequenceNumber, workOrderNo, routeCode, stationCode, createTime } = this.rowData;
      if (!this.isAuto) {
        return false;
      }
      this.loading=true;
      getDetail({
        sequenceNumber: productSequenceNumber,
        workOrderNo,
        routeCode,
        stationCode,
        inputBadTimeStr: createTime,
        // sequenceNumber:"2321WO2023032700000500000040",
        // workOrderNo:"WO20230327000005",

      })
        .then((res) => {
          let data = res.data.data;
          this.testDataDetail = data;
          let result = this._get(res, 'data.data.resultList[0].resultJson', '');
          if (result && result != null) {
            result = JSON.parse(result);
            for (const key in result) {
              this.testDataDetailListOption.column.push({
                label: key,
                prop: key
              });
            }
            this._forEach(res.data.data.resultList, (item) => {
              if (item.resultJson && item.resultJson != null) {
                this.testDataDetailResult.push(JSON.parse(item.resultJson));
              }
            });
          }
          
          this._forEach(data.resultList, (item) => {
            let attachmentsHtml = item.attachments
            if (attachmentsHtml && attachmentsHtml != null) {
              attachmentsHtml = JSON.parse(attachmentsHtml);
              console.log(attachmentsHtml, 'attachmentsHtml')
              this._forEach(attachmentsHtml, (attachment, index) => {
                this.fileListHtml 
                +=
                    `<div style="margin: 5px 10px">
                      <image src="${attachment.link}" onclick="openPreview('0', '${attachment.link}')" style="width: 100px;height:100px"></image>
                      <div>${attachment.originalName}</div>
                      <button type="button" class="el-button el-button--text el-button--small downLoadFile${index}" onclick="handleDownload('${attachment.link}')"><i class="el-icon-download"></i><span>下载</span></button>
                    </div>`;
          
              })
             
            }
          })
        })
        .catch(() => {})
        .finally(()=>this.loading=false);
    },
  }
};
</script>

<style scoped lang="scss">
.testDataContent{
  min-height: 450px;
  ::v-deep .avue-crud__menu {
    display: none;
  }
  ::v-deep .el-form {
    .avue-form__menu {
      display: none !important;
    }
  }
}
.custom-title {
  font-size: 14px;
  font-weight: bold;
  margin: 10px 0;
  i {
    margin-right: 10px;
  }
}

.dialog {
  ::v-deep .el-form {
    .avue-form__menu {
      display: none !important;
    }
  }
}

::v-deep .el-table .header-color {
  color: rgba(0, 0, 0, 0.85) !important;
}

::v-deep .el-table .scan-entry-row {
  background: #caf982;
}

.footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
::v-deep .el-card__body {
  padding: 0;
}
::v-deep .el-tabs__item {
  color: #606266 !important;
  font-weight: bold;
}
</style>
